﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class FundsOperatingTypeController : Controller
    {
        private FundsOperatingTypeInfo fundinfo = new FundsOperatingTypeInfo(true);
        public ActionResult FundsOperatingTypeIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取增减款类别列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetFundsOperatingTypeList(PageVM pvm)
        {
            string query = " and 1=1 ";
            IList<FundsOperatingTypeInfo> fundlist = fundinfo.SelFundsOperatingTypeInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = fundinfo._FundsOperatingTypecount;
            List<string> v;
            foreach (FundsOperatingTypeInfo item in fundlist)
            {

                v = new List<string>();
                v.Add(item.DepositTypeID.ToString());
                v.Add(item.DepositName);
                v.Add(item.AccountName);
                v.Add(item.bUseName);
                v.Add(item.bShowName);

                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.DepositTypeID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 删除增减款类别
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult DeleteFundsOperatingType(string ID)
        {
            int result = 0;
            result = fundinfo.DeleteFundsOperatingType(ID);
            return Json(result);
        }

        /// <summary>
        /// 新增增减款类别
        /// </summary>
        /// <returns></returns>
        public ActionResult AddFundsOperatingType()
        {
            ViewData["Account"] = GetAccount();
            ViewData["bUse"] = GetbUse();
            return View();
        }

        /// <summary>
        /// 获取类别列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetAccount()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(0, "增款");
            dic.Add(1, "减款");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem()
                {
                    Text = m.Value.ToString(),
                    Value = m.Key.ToString()
                }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取类别列表（用于在编辑下绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetAccountEdit()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(0, "增款");
            dic.Add(1, "减款");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem()
                {
                    Text = m.Value.ToString(),
                    Value = m.Key.ToString(),
                    Selected = m.Key == fundinfo.Account ? true : false
                }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取是否使用中列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetbUse()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "是");
            dic.Add(0, "否");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem()
                {
                    Text = m.Value.ToString(),
                    Value = m.Key.ToString()
                }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取是否使用中列表（用于编辑下绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetbUseEdit()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "是");
            dic.Add(0, "否");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem()
                {
                    Text = m.Value.ToString(),
                    Value = m.Key.ToString(),
                    Selected = m.Key == fundinfo.bUse ? true : false
                }).ToList();
            return selectList;
        }

        /// <summary>
        /// 保存增减款类别（新增界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveFundsOperatingType(FundsOperatingTypeVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                FundsOperatingTypeInfo fun = new FundsOperatingTypeInfo()
                {
                    DepositTypeID = model.DepositTypeID,
                    DepositName = model.DepositName,
                    Account = model.Account,
                    bUse = model.bUse,
                    blBShow = model.blBShow
                };
                result = fundinfo.SaveFundsOperatingType(fun);
            }
            return Json(result);
        }

        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult EditFundsOperatingType(string ID)
        {
            ViewData["Account"] = GetAccount();
            ViewData["bUse"] = GetbUse();
            FundsOperatingTypeVM vm = new FundsOperatingTypeVM();
            FundsOperatingTypeInfo info = new FundsOperatingTypeInfo();
            fundinfo = info.GetFundsOperatingTypeInfoOne(ID);
            vm.AddEdit = "Edit";
            vm.OldDepositTypeID = fundinfo.DepositTypeID;
            //vm.DepositTypeID = fundinfo.DepositTypeID;
            //vm.DepositName = fundinfo.DepositName;
            //vm.Account = fundinfo.Account;
            //vm.bUse = fundinfo.bUse;
            Mapper.DynamicMap(fundinfo, vm);
            return View(vm);
        }

        /// <summary>
        /// 保存增减款类别（修改界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveFundsOperatingTypeEdit(FundsOperatingTypeVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                FundsOperatingTypeInfo fund = new FundsOperatingTypeInfo()
                {
                    DepositTypeID = model.DepositTypeID,
                    DepositName = model.DepositName,
                    Account = model.Account,
                    bUse = model.bUse,
                    blBShow = model.blBShow
                };
                result = fundinfo.SaveFundsOperatingTypeEdit(fund);
            }
            return Json(result);
        }
        /// <summary>
        /// 检查是否为系统保留项
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ActionResult SelFundsOperatingTypeEdit(string ID)
        {
            int result = 0;
            result = fundinfo.SelFundsOperatingTypeEdit(ID);
            return Json(result);
        }

        /// <summary>
        /// 增减款类别代号验证
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckFundsOperatingType(int DepositTypeID)
        {
            bool istongguo = fundinfo.CheckFundsOperatingType(DepositTypeID);
            return Json(istongguo);
        }
    }
}
